<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div th:insert="common.html"></div>
<div id="toolbar" shiro:hasPermission="H">
    <input type="button" onclick="insertInfo()" class="btn btn-info" value="增加"/>
</div>
    <table id="table"></table>
</body>
</html>
<script>
    $(function (){
        $('#table').bootstrapTable({
            //请求地址
            url: '/role/getInfo',
            method : "GET",
            striped: true,//是否显示行间隔色
            cache: false,//是否使用缓存，默认为true，所以一般情况下需要设置一下这个属性
            pagination: true,//是否显示分页（*）
            /* sidePagination: "client",*/           //分页方式：client客户端分页，server服务端分页（*）
            strictSearch: true,//严格搜索
            showColumns: true,                  //是否显示所有的列（选择显示的列）
            showRefresh: true,                  //是否显示刷新按钮
            clickToSelect: true,                //是否启用点击选中行
            //列
            columns: [
                {
                    checkbox: true,
                    visible: true                  //是否显示复选框

                },{
                    //实体类属性
                    field: 'roleId',
                    //表头
                    title: 'ID'
                }, {
                    field: 'roleName',
                    title: '角色名称'
                },{
                    field: 'roleId',
                    title: "操作",
                    width:120,
                    align : "center",
                    formatter : function (value, row, index) {
                        var id = row.roleId;
                        var result = "";
                        result += "<a href='javascript:toUpdate("+id+")' class='btn btn-xs blue'  title='编辑'><span class='glyphicon glyphicon-pencil'></span></a>";
                        result += "<a href='javascript:deleteInfoById("+id+")' class='btn btn-xs red'  title='删除'><span class='glyphicon glyphicon-remove'></span></a>";

                        return result;
                    }
                }
            ]
        });
    })

    function deleteInfoById(roleId){
        if(confirm("确认删除吗?")){
            $.ajax({
                url : "/role/deleteById?roleId="+roleId,
                type : "post",
                dataType : "json",
                success : function (data){
                    toastr. success("删除成功");
                    getData();
                },
                error : function (data){
                    alert("网络连接失败")
                }
            })
        }
    }

    function toUpdate(roleId){
        BootstrapDialog.show({
            title: '修改',
            message: $('<div></div>').load('/role/toUpdate?roleId='+roleId),
            size: BootstrapDialog.SIZE_WIDE,
            buttons: [{
                label: '取消',
                action: function (dialog) {
                    dialog.close();
                }
            }, {
                label: '确定',
                action: function (dialog) {
                    var treeObj = $.fn.zTree.getZTreeObj("treeDemo");
                    var nodes = treeObj.getCheckedNodes(true);
                    var menuId = [];
                    if(nodes.length <= 0){
                        toastr.error("请选择权限")
                        return;
                    }
                    for (var i = 0; i < nodes.length; i++) {
                        menuId.push(nodes[i].id);
                    }
                    $.ajax({
                        url: "/role/updateInfo",
                        type: "post",
                        dataType: "json",
                        data: $("#update_form").serialize() + "&menuId=" + menuId,
                        success: function (data) {
                            toastr.success("修改成功");
                            dialog.close();
                            getData();
                        },
                        error: function (data) {
                            alert("网络连接失败")
                        }
                    })
                }
            }]
        });
    }


    function insertInfo() {
        BootstrapDialog.show({
            title: '提示',
            message: $('<div></div>').load('/role/toAdd'),
            size: BootstrapDialog.SIZE_WIDE,
            buttons: [{
                label: '取消',
                action: function (dialog) {
                    dialog.close();
                }
            }, {
                label: '确定',
                action: function (dialog) {
                    var treeObj = $.fn.zTree.getZTreeObj("treeDemo");
                    var nodes = treeObj.getCheckedNodes(true);
                    var mids = [];
                    if(nodes.length <= 0){
                        toastr.error("请选择权限")
                        return;
                    }
                    for (var i = 0; i < nodes.length; i++) {
                        mids.push(nodes[i].id);
                    }
                    $.ajax({
                        url: "/role/addInfo",
                        type: "post",
                        dataType: "json",
                        data: $("#add_form").serialize() + "&menuId=" + mids,
                        success: function (data) {
                            toastr.success("增加成功");
                            dialog.close();
                            getData();
                        },
                        error: function (data) {
                            alert("网络连接失败")
                        }
                    })
                }
            }]
        });
    }
    function getData(){
        $("#table").bootstrapTable("refresh")
    }
</script>